// store命名
import { StoreName } from '@store/store-name';

// 用户模块
export const useDictionaryStore = defineStore(StoreName.DICTIONARY, {
    state: (): {
        dictionary: Dictionary[];
    } => ({
        dictionary: [],
    }),
    actions: {
        getDictionaryUseField(field: string): { definition: string; width: number; type: TableType; roles: string[] } {
            for (let index = 0; index < this.dictionary.length; index++) {
                const element = this.dictionary[index];
                if (element.word == field) {
                    return {
                        definition: element.definition,
                        roles: element.roles,
                        width: element.width,
                        type: element.type,
                    };
                }
            }
            return { definition: '', roles: [], width: 0, type: '' as TableType };
        },
        getDefinition(field: string) {
            for (let index = 0; index < this.dictionary.length; index++) {
                const element = this.dictionary[index];
                if (element.word == field) {
                    return element.definition;
                }
            }
        },
        getWidth(field: string): number {
            for (let index = 0; index < this.dictionary.length; index++) {
                const element = this.dictionary[index];
                if (element.word == field) {
                    return element.width;
                }
            }
            return 0;
        },
        getType(field: string): TableType {
            for (let index = 0; index < this.dictionary.length; index++) {
                const element = this.dictionary[index];
                if (element.word == field) {
                    return element.type;
                }
            }
            return '' as TableType;
        },
        getRoles(field: string): string[] {
            for (let index = 0; index < this.dictionary.length; index++) {
                const element = this.dictionary[index];
                if (element.word == field) {
                    return element.roles;
                }
            }
            return [];
        },
    },
    persist: {
        storage: sessionStorage,
    },
});
